<template>
    <div class="container-content">
        <el-form
            ref="formRef"
            :model="formObject"
            label-width="100px"
            :rules="rules"
            status-icon
            label-position="left"
        >
            <el-collapse v-model="activeName" @change="handleClick">
                <el-collapse-item title="基本" name="basic">
                    <el-row :gutter="15">
                        <el-col v-if="rowId" :span="8">
                            <el-form-item label="单据类型" prop="docType">
                                <el-input
                                    :disabled="true"
                                    style="width: 100%"
                                    v-model="formObject.docType"
                                    :props="props"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col v-if="!rowId" :span="8">
                            <el-form-item label="单据类型" prop="docType">
                                <el-select
                                    v-model="formObject.docType"
                                    placeholder="请选择"
                                    style="width: 100%"
                                    @change="docTypeChange"
                                >
                                    <el-option
                                        v-for="item in documentTypeList"
                                        :key="item.keyCode"
                                        :label="item.title"
                                        :value="item.keyCode"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label="业务类型"
                                prop="docBusinessType"
                            >
                                <el-select
                                    v-model="formObject.docBusinessType"
                                    placeholder="请选择"
                                    style="width: 100%"
                                    disabled
                                >
                                    <el-option
                                        v-for="item in documentTypeList"
                                        :key="item.keyCode"
                                        :label="item.title"
                                        :value="item.keyCode"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="单据编码" prop="docCode">
                                <el-input
                                    v-model="formObject.docCode"
                                    placeholder="保存时自动生成"
                                    clearable
                                    :style="{ width: '100%' }"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="询价日期" prop="askTime">
                                <el-date-picker
                                    v-model="formObject.askTime"
                                    format="yyyy-MM-dd"
                                    value-format="yyyy-MM-dd"
                                    :style="{ width: '100%' }"
                                    placeholder="请输入日期"
                                    clearable
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>

                        <el-col :span="8">
                            <el-form-item label="比价模式" prop="compareMode">
                                <el-select
                                    v-model="formObject.compareMode"
                                    placeholder="请选择"
                                    style="width: 100%"
                                >
                                    <el-option
                                        v-for="item in compareModeList"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :span="8">
                            <el-form-item label="单据状态" prop="docStatus">
                                <el-select
                                    v-model="formObject.docStatus"
                                    placeholder=""
                                    style="width: 100%"
                                    disabled
                                >
                                </el-select>
                            </el-form-item>
                        </el-col>

                        <el-col :span="8">
                            <el-form-item
                                label-width="120px"
                                label="报价截止日期"
                                prop="quotationDeadline"
                            >
                                <el-date-picker
                                    v-model="formObject.quotationDeadline"
                                    format="yyyy-MM-dd"
                                    value-format="yyyy-MM-dd"
                                    :style="{ width: '100%' }"
                                    placeholder="请输入日期"
                                    clearable
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label-width="120px"
                                label="价格生效日期"
                                prop="priceEffectiveDate"
                            >
                                <el-date-picker
                                    v-model="formObject.priceEffectiveDate"
                                    format="yyyy-MM-dd"
                                    value-format="yyyy-MM-dd"
                                    :style="{ width: '100%' }"
                                    placeholder="请输入日期"
                                    clearable
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label-width="120px"
                                label="价格失效日期"
                                prop="priceExpirationDate"
                            >
                                <el-date-picker
                                    v-model="formObject.priceExpirationDate"
                                    format="yyyy-MM-dd"
                                    value-format="yyyy-MM-dd"
                                    :style="{ width: '100%' }"
                                    placeholder="请输入日期"
                                    clearable
                                ></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label="结算币别"
                                prop="settlementCurrencyName"
                            >
                                <el-input
                                    v-model="formObject.settlementCurrencyName"
                                    placeholder="请输入结算币别"
                                    clearable
                                >
                                    <i
                                        slot="suffix"
                                        class="el-input__icon el-icon-notebook-2"
                                        style="cursor: pointer"
                                        @click="addDialog('currency')"
                                    ></i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label="付款条件"
                                prop="payConditionName"
                            >
                                <el-input
                                    v-model="formObject.payConditionName"
                                    placeholder="请输入付款条件"
                                    clearable
                                    :style="{ width: '100%' }"
                                >
                                    <i
                                        slot="suffix"
                                        class="el-input__icon el-icon-notebook-2"
                                        style="cursor: pointer"
                                        @click="addDialog('payCondition')"
                                    ></i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="询价单名称" prop="inquiryName">
                                <el-input
                                    v-model="formObject.inquiryName"
                                    placeholder="请输入询价单名称"
                                    clearable
                                    :style="{ width: '100%' }"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="采购组织" prop="purchaseOrg">
                                <el-input
                                    v-model="formObject.purchaseOrg"
                                    placeholder=""
                                    clearable
                                    disabled
                                    :style="{ width: '100%' }"
                                >
                                    <!-- <i
                                        slot="suffix"
                                        class="el-input__icon el-icon-notebook-2"
                                        style="cursor: pointer"
                                        @click="addDialog('saleDept')"
                                    ></i> -->
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label="采购部门"
                                prop="purchaseDeptName"
                            >
                                <el-input
                                    v-model="formObject.purchaseDeptName"
                                    placeholder="请选择采购部门"
                                    clearable
                                    :style="{ width: '100%' }"
                                >
                                    <i
                                        slot="suffix"
                                        class="el-input__icon el-icon-notebook-2"
                                        style="cursor: pointer"
                                        @click="addDialog('dept')"
                                    ></i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label="采购组"
                                prop="purchaseGroupName"
                            >
                                <el-input
                                    v-model="formObject.purchaseGroupName"
                                    placeholder="请选择采购组"
                                    clearable
                                    :style="{ width: '100%' }"
                                >
                                    <i
                                        slot="suffix"
                                        class="el-input__icon el-icon-notebook-2"
                                        style="cursor: pointer"
                                        @click="addDialog('deptGroup')"
                                    ></i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="本位币" prop="baseCurrency">
                                <el-input
                                    v-model="formObject.baseCurrency"
                                    placeholder=""
                                    clearable
                                    disabled
                                    :style="{ width: '100%' }"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label="结算方式"
                                prop="settlementMethodName"
                            >
                                <el-input
                                    v-model="formObject.settlementMethodName"
                                >
                                    <i
                                        slot="suffix"
                                        class="el-input__icon el-icon-notebook-2"
                                        style="cursor: pointer"
                                        @click="addDialog('settlementType')"
                                    ></i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="采购员" prop="purchaseByName">
                                <el-input
                                    v-model="formObject.purchaseByName"
                                    placeholder="请选择采购员"
                                    clearable
                                    :style="{ width: '100%' }"
                                >
                                    <i
                                        slot="suffix"
                                        class="el-input__icon el-icon-notebook-2"
                                        style="cursor: pointer"
                                        @click="addDialog('saleId')"
                                    ></i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label="汇率类型"
                                prop="exchangeRateTypeName"
                            >
                                <el-input
                                    v-model="formObject.exchangeRateTypeName"
                                    placeholder="请选择汇率类型"
                                    clearable
                                    :style="{ width: '100%' }"
                                >
                                    <i
                                        slot="suffix"
                                        class="el-input__icon el-icon-notebook-2"
                                        style="cursor: pointer"
                                        @click="addDialog('rateType')"
                                    ></i>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="价格类型" prop="priceType">
                                <el-select
                                    v-model="formObject.priceType"
                                    placeholder="请选择"
                                    style="width: 100%"
                                    disabled
                                >
                                    <el-option
                                        v-for="item in priceTypeList"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="汇率">
                                <el-input
                                    v-model="formObject.exchangeRate"
                                    clearable
                                    disabled
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>

                        <el-col :span="8">
                            <el-form-item label="是否含税" prop="isTax">
                                <el-radio
                                    v-model="formObject.isTax"
                                    :label="false"
                                    border
                                    >否</el-radio
                                >
                                <el-radio
                                    v-model="formObject.isTax"
                                    :label="true"
                                    border
                                    >是</el-radio
                                >
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label-width="160px"
                                label="是否适用所有供应商"
                                prop="isAll"
                            >
                                <el-radio
                                    v-model="formObject.isAll"
                                    :label="false"
                                    border
                                    >否</el-radio
                                >
                                <el-radio
                                    v-model="formObject.isAll"
                                    :label="true"
                                    border
                                    >是</el-radio
                                >
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label-width="200px"
                                label="允许比价时采纳多个供应商"
                                prop="isAllowAdoptingSuppliers"
                            >
                                <el-radio
                                    v-model="
                                        formObject.isAllowAdoptingSuppliers
                                    "
                                    :label="false"
                                    border
                                    >否</el-radio
                                >
                                <el-radio
                                    v-model="
                                        formObject.isAllowAdoptingSuppliers
                                    "
                                    :label="true"
                                    border
                                    >是</el-radio
                                >
                            </el-form-item>
                        </el-col>

                        <el-col :span="24">
                            <el-form-item label="备注" prop="remark">
                                <el-input
                                    v-model="formObject.remark"
                                    placeholder="请输入备注"
                                    clearable
                                    type="textarea"
                                    :style="{ width: '100%' }"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-collapse-item>
                <el-collapse-item title="物料数据" name="material">
                    <el-row :gutter="15"> </el-row>
                </el-collapse-item>
                <el-collapse-item title="明细信息" name="detail">
                    <el-button
                        type="text"
                        size="medium"
                        @click="addNewRow('inquiryDetailList')"
                        >新增行</el-button
                    >
                    <el-table
                        :data="formObject.inquiryDetailList"
                        style="width: 100%"
                        highlight-current-row
                        border
                    >
                        <el-table-column label="操作" width="60">
                            <template slot-scope="scope">
                                <el-button
                                    plain
                                    icon="el-icon-minus"
                                    size="mini"
                                    @click="
                                        handleDataDelete(
                                            scope.$index,
                                            scope.row,
                                            'inquiryDetailList'
                                        )
                                    "
                                ></el-button>
                            </template>
                        </el-table-column>
                        <el-table-column
                            type="index"
                            label="序号"
                            width="55"
                            align="center"
                        ></el-table-column>
                        <el-table-column
                            width="200"
                            label=""
                            prop="materialCode"
                        >
                            <template slot="header">
                                物料编码<span class="required">*</span>
                            </template>
                            <template slot-scope="scope">
                                <el-form-item
                                    label-width="0px"
                                    style="margin: 0"
                                    :rules="rules.materialCode"
                                    :prop="
                                        'inquiryDetailList.' +
                                        scope.$index +
                                        '.materialCode'
                                    "
                                >
                                    <el-input
                                        v-model="scope.row.materialCode"
                                        clearable
                                    >
                                        <i
                                            slot="suffix"
                                            @click="
                                                addDialog(
                                                    'material',
                                                    scope.$index
                                                )
                                            "
                                            class="el-input__icon el-icon-notebook-2"
                                            style="cursor: pointer"
                                        ></i>
                                    </el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="物料名称" prop="materialName">
                            <template slot-scope="scope">
                                <el-form-item
                                    label-width="0px"
                                    style="margin: 0"
                                >
                                    <el-input
                                        v-model="scope.row.materialName"
                                        clearable
                                    ></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="规格型号" prop="materialModel">
                            <template slot-scope="scope">
                                <el-form-item
                                    prop="materialModel"
                                    label-width="0px"
                                    style="margin: 0"
                                >
                                    <el-input
                                        v-model="scope.row.materialModel"
                                        disabled
                                    ></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="" prop="unitName">
                            <template slot="header">
                                询价单位<span class="required">*</span>
                            </template>
                            <template slot-scope="scope">
                                <el-form-item
                                    label-width="0px"
                                    style="margin: 0"
                                    :rules="rules.unit"
                                    :prop="
                                        'inquiryDetailList.' +
                                        scope.$index +
                                        '.unitName'
                                    "
                                >
                                    <el-input v-model="scope.row.unitName">
                                        <i
                                            slot="suffix"
                                            @click="
                                                addDialog('unit', scope.$index)
                                            "
                                            class="el-input__icon el-icon-notebook-2"
                                            style="cursor: pointer"
                                        ></i
                                    ></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="询价数量" prop="num">
                            <template slot-scope="scope">
                                <el-form-item
                                    prop="num"
                                    label-width="0px"
                                    style="margin: 0"
                                >
                                    <el-input
                                        v-model="scope.row.num"
                                    ></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="到货日期"
                            prop="arrivalTime"
                            width="150"
                        >
                            <template slot-scope="scope">
                                <el-form-item
                                    prop="arrivalTime"
                                    label-width="0px"
                                    style="margin: 0"
                                >
                                    <el-date-picker
                                        v-model="scope.row.arrivalTime"
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        :style="{ width: '100%' }"
                                        placeholder="请输入日期"
                                        clearable
                                    ></el-date-picker>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="交货方式" prop="deliveryMethod">
                            <template slot-scope="scope">
                                <el-form-item
                                    label-width="0px"
                                    style="margin: 0"
                                >
                                    <el-select
                                        v-model="scope.row.deliveryMethod"
                                        placeholder="请选择"
                                        style="width: 100%"
                                    >
                                        <el-option
                                            v-for="item in deliveryMethodList"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="交货地址"
                            prop="deliveryAddress"
                        >
                            <template slot-scope="scope">
                                <el-form-item
                                    prop="deliveryAddress"
                                    label-width="0px"
                                    style="margin: 0"
                                >
                                    <el-input
                                        v-model="scope.row.deliveryAddress"
                                        clearable
                                    ></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column
                            label="参考币别"
                            prop="referenceCurrency"
                        >
                            <template slot-scope="scope">
                                <el-form-item
                                    prop="referenceCurrency"
                                    label-width="0px"
                                    style="margin: 0"
                                >
                                    <el-input
                                        v-model="scope.row.referenceCurrency"
                                        clearable
                                        disabled
                                    ></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="参考价" prop="referencePrice">
                            <template slot-scope="scope">
                                <el-form-item
                                    prop="referencePrice"
                                    label-width="0px"
                                    style="margin: 0"
                                >
                                    <el-input
                                        v-model="scope.row.referencePrice"
                                        clearable
                                        disabled
                                    ></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                        <el-table-column label="备注" prop="remark">
                            <template slot-scope="scope">
                                <el-form-item
                                    prop="remark"
                                    label-width="0px"
                                    style="margin: 0"
                                >
                                    <el-input
                                        v-model="scope.row.remark"
                                        clearable
                                    ></el-input>
                                </el-form-item>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-collapse-item>
            </el-collapse>
        </el-form>
        <el-dialog
            :title="dialogTitle"
            :visible.sync="dialog"
            append-to-body
            width="70%"
            destroy-on-close
            v-if="dialog"
        >
            <component
                :is="currentCom"
                :id="rowId"
                :visible.sync="dialog"
                @getSelectVal="getSelectVal"
            ></component>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelSelect">取 消</el-button>
                <el-button type="primary" @click="confirmSelect"
                    >确 定</el-button
                >
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { listDept } from "@/api/system/dept";
import { getAreaList } from "@/api/area/area";
import {
    getOrderDetail,
    addList,
    getDocTypeList,
    addInquiryList,
} from "@/api/system/purchase";
import { addQuotationList } from "@/api/system/sale";
import { getCompute } from "@/api/finance/info";

import SaleId from "@/components/dialog/saleId";
import Customer from "@/components/dialog/customer";
import BusinessGroup from "@/components/dialog/businessGroup";
import QueryMaterialList from "@/components/dialog/queryMaterialList";
import PriceUnit from "@/components/dialog/priceUnit";
import TaxRate from "@/components/dialog/taxRate";

import Currency from "@/components/dialog/currency";
import paymentTerms from "@/components/dialog/paymentTerms";
import Dept from "@/components/dialog/dept";
import settlementType from "@/components/dialog/settlementType";
import rateType from "@/components/dialog/rateType";

export default {
    props: ["rowId"],
    components: {
        SaleId,
        Customer,
        Dept,
        BusinessGroup,
        QueryMaterialList,
        PriceUnit,
        TaxRate,
        Currency,
        paymentTerms,
        settlementType,
        rateType,
    },
    data() {
        return {
            activeName: ["basic", "detail"],
            dialog: false,
            counties: [],
            orgData: [], //创建组织弹窗列表
            orgSelect: [], //选中项
            selectItem: [],
            orgTotal: "", //创建组织弹窗分页
            dialogType: "",
            dialogIndex: "",
            currentCom: "", //弹窗组件
            dialogTitle: "", //弹窗标题
            props: {},
            formObject: {
                askCode: "",
                askTime: "",
                baseCurrency: "",
                closeBy: "",
                closeStatus: "",
                closeTime: "",
                compareMode: "",
                confirmBy: "",
                confirmStatus: "",
                contracts: "",
                createBy: "",
                createTime: "",
                docBusinessType: "",
                docCode: "",
                docStatus: "",
                docType: "",
                email: "",
                examineBy: "",
                examineTime: "",
                exchangeRate: "",
                exchangeRateType: "",
                id: "",
                inquiryName: "",
                isAll: true,
                isAllowAdoptingSuppliers: true,
                isExtraPriceTax: true,
                isInternal: true,
                isTax: true,

                payCondition: "",
                phone: "",
                priceEffectiveDate: "",
                priceExpirationDate: "",
                priceType: "1",
                purchaseBy: "",
                purchaseDept: "",
                purchaseGroup: "",
                purchaseOrg: "",
                quotationDeadline: "",
                remark: "",
                settlementCurrency: "",
                settlementCurrencyName: "",
                settlementMethod: "",
                supplierCode: "",
                thirdMessage: true,
                thirdTemplate: "",
                type: "0",
                updateBy: "",
                updateTime: "",
                businessType: "34",

                modelId: this.$store.state.workflow.businessConfig.id,

                inquiryDetailList: [],
            },
            rules: {
                docType: [
                    {
                        required: true,
                        message: "请选择单据类型",
                        trigger: "change",
                    },
                ],
                priceExpirationDate: [
                    {
                        required: true,
                        message: "请选择价格失效日期",
                        trigger: "change",
                    },
                ],
                quotationDeadline: [
                    {
                        required: true,
                        message: "请选择报价截止日期",
                        trigger: "change",
                    },
                ],
                priceEffectiveDate: [
                    {
                        required: true,
                        message: "请选择价格生效日期",
                        trigger: "change",
                    },
                ],
                settlementCurrencyName: [
                    {
                        required: true,
                        message: "请选择结算币别",
                        trigger: "blur",
                    },
                ],
                unitName: [
                    {
                        required: true,
                        message: "请选择询价单位",
                        trigger: "blur",
                    },
                ],
            },
            documentTypeList: [],
            compareModeList: [],
            priceTypeList: [
                {
                    label: "采购",
                    value: "1",
                },
                {
                    label: "委外",
                    value: "2",
                },
                {
                    label: "VMI",
                    value: "3",
                },
                {
                    label: "工序委外",
                    value: "4",
                },
                {
                    label: "工序协作",
                    value: "5",
                },
            ],
        };
    },
    methods: {
        // 页签切换事件
        handleClick(tab, event) {},
        // 附件上传
        handleFileChange() {
            console.log("文件上传");
        },
        // 单据类型选择change事件
        docTypeChange(value) {
            if (value) {
                this.formObject.docBusinessType = value;
            }
        },
        // 基础表单提交
        submit() {
            this.$refs["formRef"].validate((valid) => {
                if (!valid) return;
                // TODO 提交表单
                addInquiryList(this.formObject).then((res) => {
                    if (res.code === 200) {
                        this.$message({
                            message: "新增成功！",
                            type: "success",
                        });
                        this.$store.commit(
                            "workflow/SET_BUSINESSDRAWER",
                            false
                        );
                    }
                });
            });
        },

        // 基础表单取消
        formRest(formRef) {
            this.$refs[formRef].resetFields();
        },
        // 删除行
        handleDataDelete(index, row, type) {
            this.$confirm("请确认是否要删除此行？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.$message({
                        type: "success",
                        message: "删除成功!",
                    });
                    if (type === "inquiryDetailList") {
                        // 明细信息
                        this.formObject.inquiryDetailList.splice(index, 1);
                    }
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },

        // 物料明细 添加行
        addInquiryDetailList(index, row) {
            // 物料明细 obj
            let detailObj = this.getDetialObj();
            this.formObject.inquiryDetailList.push(detailObj);
        },

        //添加新行
        addNewRow(type) {
            if (type === "inquiryDetailList") {
                this.addInquiryDetailList(); //明细信息
            }
        },
        // 处理弹窗弹出
        handleOpen() {
            const nameMap = {
                customer: () => {
                    this.dialogTitle = "选择客户";
                    this.currentCom = Customer;
                },
                dept: () => {
                    this.dialogTitle = "选择部门";
                    this.currentCom = Dept;
                },

                saleId: () => {
                    this.dialogTitle = "选择采购员";
                    this.currentCom = SaleId;
                },
                deptGroup: () => {
                    this.dialogTitle = "选择采购组";
                    this.currentCom = BusinessGroup;
                },
                material: () => {
                    this.dialogTitle = "选择物料";
                    this.currentCom = QueryMaterialList;
                },
                unit: () => {
                    this.dialogTitle = "选择单位";
                    this.currentCom = PriceUnit;
                },
                priceUnit: () => {
                    this.dialogTitle = "选择单位";
                    this.currentCom = PriceUnit;
                },
                taxRate: () => {
                    this.dialogTitle = "选择税率";
                    this.currentCom = TaxRate;
                },
                currency: () => {
                    this.dialogTitle = "选择币别";
                    this.currentCom = Currency;
                },
                payCondition: () => {
                    this.dialogTitle = "选择付款条件";
                    this.currentCom = paymentTerms;
                },
                settlementType: () => {
                    this.dialogTitle = "选择结算方式";
                    this.currentCom = settlementType;
                },
                rateType: () => {
                    this.dialogTitle = "选择汇率类型";
                    this.currentCom = rateType;
                },
            };
            nameMap[this.dialogType] ? nameMap[this.dialogType]() : "";
        },
        // 选择弹窗 item
        getSelectVal(val) {
            this.selectItem = val;
        },
        //点击选择dialog中的一条数据回显
        addDialog(type, index) {
            this.dialog = true;
            this.dialogType = type;
            this.dialogIndex = index;
            this.handleOpen();
        },

        // 创建组织
        selectOrg() {
            this.showOrg = true;
            let level = 1;
            listDept({ level }).then((res) => {
                if (res.code === 200) {
                    this.orgData = res.data;
                }
                return;
            });
        },
        // 创建组织item
        selectOrgItem(val) {
            if (val.length === 1) {
                this.orgSelect = val;
                this.showOrg = false;
            }
        },
        //获取单据类型
        getDocTypeListApi() {
            getDocTypeList({ docCode: "SVM_InquiryBill" }).then((res) => {
                if (res.code === 200) {
                    this.documentTypeList = res.rows;
                }
            });
        },
        // 获取比价模式字典
        getCompareModeList() {
            // 比价模式
            this.getDicts("sys_compare_mode").then((res) => {
                if (res.code === 200) {
                    this.compareModeList = res.data.map((item) => {
                        return {
                            value: item.dictCode,
                            label: item.dictLabel,
                        };
                    });
                }
            });
            // 交货方式
            this.getDicts("delivery_method").then((res) => {
                if (res.code === 200) {
                    this.deliveryMethodList = res.data.map((item) => {
                        return {
                            value: item.dictCode,
                            label: item.dictLabel,
                        };
                    });
                }
            });
        },
        //下拉类型选择
        handleCommand(val) {
            this.dialogType = val;
            this.dialog = true;
            this.handleOpen();
        },

        cancelSelect() {
            this.dialog = false;
            this.selectItem = [];
        },
        confirmSelect() {
            const nameMap = {
                customer: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条数据！");
                        return;
                    }
                    this.formObject.customerId = this.selectItem[0].id;
                    this.formObject.customerName =
                        this.selectItem[0].customerName;
                    this.dialog = false;
                },

                dept: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条数据！");
                        return;
                    }
                    this.formObject.purchaseDept = this.selectItem[0].deptId;
                    this.$set(
                        this.formObject,
                        "purchaseDeptName",
                        this.selectItem[0].deptName
                    );
                    this.$set(
                        this.formObject,
                        "purchaseDeptCode",
                        this.selectItem[0].deptCode
                    );
                    this.dialog = false;
                },
                saleId: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条数据！");
                        return;
                    }
                    this.formObject.purchaseBy = this.selectItem[0].userId;
                    this.$set(
                        this.formObject,
                        "purchaseByName",
                        this.selectItem[0].userName
                    );
                    this.dialog = false;
                },
                deptGroup: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条数据！");
                        return;
                    }
                    this.formObject.purchaseGroup = this.selectItem[0].id;
                    this.$set(
                        this.formObject,
                        "purchaseGroupName",
                        this.selectItem[0].businessGroupType
                    );
                    this.dialog = false;
                },
                material: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条数据！");
                        return;
                    }
                    this.selectItem.forEach((item) => {
                        let detailObj = this.getDetialObj();
                        Object.keys(item).forEach((itemKey) => {
                            Object.keys(detailObj).forEach((detailKey) => {
                                if (itemKey === detailKey) {
                                    detailObj[detailKey] = item[itemKey];
                                }
                                if (itemKey === "id") {
                                    detailObj["materialId"] = item[itemKey];
                                }
                                if (itemKey === "model") {
                                    detailObj["materialModel"] = item[itemKey];
                                }
                                if (itemKey === "purchaseUnitName") {
                                    detailObj["unitName"] = item[itemKey];
                                }
                                if (itemKey === "purchaseUnit") {
                                    detailObj["unit"] = item[itemKey];
                                }
                            });
                        });
                        this.$set(
                            this.formObject.inquiryDetailList,
                            this.dialogIndex,
                            detailObj
                        );
                    });
                    this.dialog = false;
                },
                unit: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条记录！");
                        return;
                    }
                    // 询价单位 unit
                    this.formObject.inquiryDetailList[this.dialogIndex].unit =
                        this.selectItem[0].id;
                    this.$set(
                        this.formObject.inquiryDetailList[this.dialogIndex],
                        "unitName",
                        this.selectItem[0].meteringUnitName
                    );
                    this.$set(
                        this.formObject.inquiryDetailList[this.dialogIndex],
                        "unitCode",
                        this.selectItem[0].meteringUnitCode
                    );

                    this.dialog = false;
                },
                priceUnit: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条记录！");
                        return;
                    }
                    // 计价单位
                    this.formObject.materialDetailEntities[
                        this.dialogIndex
                    ].pricingUnit = this.selectItem[0].meteringUnitCode;
                    this.formObject.materialDetailEntities[
                        this.dialogIndex
                    ].pricingUnitName = this.selectItem[0].meteringUnitName;
                    this.dialog = false;
                },
                taxRate: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条记录！");
                        return;
                    }
                    // 税率
                    this.formObject.materialDetailEntities[
                        this.dialogIndex
                    ].taxRate = this.selectItem[0].rate;
                    this.caculateNum(
                        this.formObject.materialDetailEntities[
                            this.dialogIndex
                        ],
                        this.dialogIndex
                    );
                    this.dialog = false;
                },
                rateType: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条记录！");
                        return;
                    }
                    this.formObject.exchangeRateType = this.selectItem[0].id;
                    this.$set(
                        this.formObject,
                        "exchangeRateTypeName",
                        this.selectItem[0].name
                    );
                    this.$set(
                        this.formObject,
                        "exchangeRateTypeCode",
                        this.selectItem[0].code
                    );
                },
                currency: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条记录！");
                        return;
                    }
                    this.formObject.settlementCurrency = this.selectItem[0].id;
                    this.$set(
                        this.formObject,
                        "settlementCurrencyName",
                        this.selectItem[0].currencyName
                    );
                    this.formObject.exchangeRate = this.selectItem[0].rate;
                    this.dialog = false;
                },
                settlementType: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条记录！");
                        return;
                    }
                    this.formObject.settlementMethod = this.selectItem[0].id;
                    this.$set(
                        this.formObject,
                        "settlementMethodName",
                        this.selectItem[0].title
                    );
                    this.$set(
                        this.formObject,
                        "settlementMethodCode",
                        this.selectItem[0].bizCode
                    );
                    this.dialog = false;
                },
                payCondition: () => {
                    if (this.selectItem.length !== 1) {
                        this.$message.error("请选择一条记录！");
                        return;
                    }
                    this.formObject.payCondition = this.selectItem[0].id;
                    this.$set(
                        this.formObject,
                        "payConditionName",
                        this.selectItem[0].title
                    );
                    this.dialog = false;
                },
            };
            nameMap[this.dialogType] ? nameMap[this.dialogType]() : "";
        },
        getDetialObj() {
            return {
                arrivalTime: "",
                deliveryMethod: "",
                id: "",
                inquiryDetailEntry: {
                    arrivalTime: "",
                    auxiliaryProperties: "",
                    compareRelatedNum: 0,
                    confirmComment: "",
                    confirmNum: 0,
                    confirmPrice: "",
                    confirmSupplier: "",
                    confirmTaxPrice: "",
                    containTaxPrice: "",
                    deliveryAddress: "",
                    deliveryMethod: "",
                    detailId: "",
                    id: "",
                    inquiryId: "",
                    inquiryNum: 0,
                    inquiryUnit: "",
                    materialCode: "",
                    materialModel: "",
                    money: "",
                    orderRelatedNum: 0,
                    price: "",
                    quotationNum: 0,
                    remark: "",
                    requireDept: "",
                    requireOrg: "",
                    result: "",
                    sourceDocCode: "",
                    sourceDocType: "",
                    tax: "",
                    taxMoney: "",
                    taxPrice: "",
                },
                inquiryId: "",

                materialCode: "",
                materialId: "",
                materialModel: "",
                materialName: "",
                num: 0,
                price: "",
                quotationNum: 0,
                referenceCurrency: "",
                referencePrice: "",
                remark: "",
                tax: "",
                taxPrice: "",
                type: "0",
                unit: "",
            };
        },
        // 计算值
        caculateNum(row, index) {
            // 数量，税率，价税合计，汇率
            console.log("触发", row);
            if (row == undefined) {
                return;
            }
            if (
                row.number &&
                row.taxRate &&
                row.totalTaxPrice &&
                this.formObject.rate
            ) {
                let totalTaxAmount = row.totalTaxPrice;
                let number = row.number;
                let taxRate = row.taxRate;
                let rate = this.formObject.rate;
                getCompute({ number, taxRate, totalTaxAmount, rate }).then(
                    (res) => {
                        this.$set(
                            this.formObject.materialDetailEntities[index],
                            "price",
                            res.data.price
                        );
                        this.$set(
                            this.formObject.materialDetailEntities[index],
                            "amountMoney",
                            res.data.amount
                        );

                        this.$set(
                            this.formObject.materialDetailEntities[index],
                            "taxAmount",
                            res.data.taxAmount
                        );

                        this.$set(
                            this.formObject.materialDetailEntities[index],
                            "taxPrice",
                            res.data.taxUnitPrice
                        );
                    }
                );
            }
        },
        getSummaries(param) {
            const { columns, data } = param;
            const sums = [];
            columns.forEach((column, index) => {
                if (index === 0) {
                    sums[index] = "总计";
                    return;
                }
                if (
                    column.property === "number" ||
                    column.property === "pricingNum" ||
                    column.property === "amountMoney" ||
                    column.property === "taxAmount" ||
                    column.property === "totalTaxPrice"
                ) {
                    const values = data.map((item) =>
                        Number(item[column.property])
                    );
                    if (!values.every((value) => isNaN(value))) {
                        sums[index] = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                return prev + curr;
                            } else {
                                return prev;
                            }
                        }, 0);
                        sums[index] += "";
                    } else {
                        sums[index] = "N/A";
                    }
                } else {
                    return;
                }
            });

            return sums;
        },
    },
    created() {
        this.getDocTypeListApi();
        this.getCompareModeList();
    },
};
</script>
<style lang="scss" scoped>
// .add {
//   padding: 15px;
// }
.panel-title {
    font-size: 18px;
    color: #37474f;
    font-weight: 500;
    text-shadow: rgba(0, 0, 0, 0.15) 0 0 1px;
}

::v-deep .el-form-item__content {
    margin-left: 0;
}

.main-responsible {
    font-size: 14px;
    font-weight: 550;
    color: #263238;
    position: relative;
    text-indent: 15px;
}
::v-deep .el-table td.el-table__cell div {
    overflow: visible;
}

::v-deep .el-table td.el-table__cell {
    padding-bottom: 20px;
}
</style>
